/*
 * Player.cpp
 *
 *  Created on: 2010-05-27
 *      Author: mateusz
 */

#include "Player.h"

#include <math.h>

//------------------------------------------------------------------------------

Player::Player(CL_GraphicContext &gc, CL_ResourceManager *resources, Map *m, Bombs *b) : Element(0,0) {
	playerImage = CL_Sprite(gc, "game/player1", resources);

	dx=0;
	dy=0;
	speed = 20;

	map = m;
	bombs = b;

	alive = true;
}

//------------------------------------------------------------------------------

Player::~Player() {
	// TODO Auto-generated destructor stub
}

//------------------------------------------------------------------------------

void Player::setImage(CL_GraphicContext &gc, CL_ResourceManager *resources, int id) {
	if( id % 4 == 0 ) {
		playerImage = CL_Sprite(gc, "game/player1", resources);
	} else if( id % 4 == 1 ) {
		playerImage = CL_Sprite(gc, "game/player2", resources);
	} else if( id % 4 == 2 ) {
		playerImage = CL_Sprite(gc, "game/player3", resources);
	} else if( id % 4 == 3 ) {
		playerImage = CL_Sprite(gc, "game/player4", resources);
	}
}

//------------------------------------------------------------------------------

void Player::attachKeyboard(CL_DisplayWindow *window) {
	slots.connect(window->get_ic().get_keyboard().sig_key_down(), this, &Player::onKeyDown);
}

//------------------------------------------------------------------------------

void Player::onKeyDown(const CL_InputEvent &key, const CL_InputState &state) {
	if( alive ) {
		if( key.type == key.pressed ) {
			if( key.id == CL_KEY_LEFT ) {
				dx = -1;
				dy = 0;
			} else if( key.id == CL_KEY_RIGHT ) {
				dx = 1;
				dy = 0;
			} else if( key.id == CL_KEY_UP ) {
				dy = -1;
				dx = 0;
			} else if ( key.id == CL_KEY_DOWN ) {
				dy = 1;
				dx = 0;
			} else if( key.id == CL_KEY_SPACE ) {
				if( bombs->addBomb(getX(),getY(),map,true) )
					signalBombPlanted.invoke(getX(),getY());
			}
		}
	}
}

//------------------------------------------------------------------------------

void Player::draw(CL_GraphicContext &gc) {
	if( alive )
		playerImage.draw(gc, getX(), getY());
}

//------------------------------------------------------------------------------

void Player::check() {
	if( map->map[getX()/20*map->size+getY()/20] >= 4 ) {
		if(alive)
			signalDead.invoke(1);
		alive = false;
	}
}

//------------------------------------------------------------------------------

void Player::move(double timePassed) {
	int newX, newY;

	newX = getX();
	newY = getY();
	newX += dx * speed;
	newY += dy * speed;

	int i = newX / 20;
	int j = newY / 20;

	if( newX >= 0 && newY >= 0 && newX <= 360 && newY <= 360 ) {
		if( map->map[i*map->size+j] == 0 ) {
			if( newX != getX() || newY != getY() ) {
				goTo(newX,newY);

				signalPlayerMove.invoke(getX(),getY());
			}
		}
	}

	dx = dy = 0;
}

//------------------------------------------------------------------------------

void Player::goToStartPosition(int id) {
	CL_Console::write("id: %1\n", id);

	if( id % 4 == 0 ) {
		goTo(0,0);
	} else if( id % 4 == 3 ) {
		goTo(0,(map->size-2)*20);
	} else if( id % 4 == 2 ) {
		goTo((map->size-2)*20,0);
	} else if( id % 4 == 1 ) {
		goTo((map->size-2)*20,(map->size-2)*20);
	}

	signalPlayerMove.invoke(getX(),getY());
}

//------------------------------------------------------------------------------
